<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <!--      <el-form-item label="请求单号" prop="requestId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.requestId"-->
      <!--          placeholder="请输入请求单号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="商户类型" prop="registerRole">
        <el-select v-model="queryParams.registerRole" placeholder="请选择商户类型" clearable>
          <el-option label="企业" value="ENTERPRISE_LEGAL_PERSON" />
          <el-option label="个体户" value="INDIVIDUAL_BUSINESS" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="signedName">
        <name-input v-model="queryParams.signedName" placeholder="请输入公司名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺名称" prop="signedShorthand">
        <el-input v-model="queryParams.signedShorthand" placeholder="请输入店铺名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="二级商户号" prop="subMerchantId">
        <el-input v-model="queryParams.subMerchantId" placeholder="请输入二级商户号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input v-model="queryParams.legalPersonName" placeholder="请输入法人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人手机号" prop="legalPersonPhone">
        <el-input v-model="queryParams.legalPersonPhone" placeholder="请输入法人手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人邮箱" prop="contactEmail">
        <el-input v-model="queryParams.contactEmail" placeholder="请输入法人邮箱" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="签约状态" prop="signingStatus">
        <el-select v-model="queryParams.signingStatus" placeholder="请选择签约状态" clearable>
          <el-option v-for="item in signingStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="所属平台" v-if="user.userId === 1" prop="platformNickName">
        <el-input v-model="queryParams.platformNickName" placeholder="请输入所属平台" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属代理" v-if="user.deptId < 101" prop="agentNickName">
        <el-input v-model="queryParams.agentNickName" placeholder="请输入所属代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属二级代理" v-if="user.deptId < 102" prop="childAgentNickName">
        <el-input v-model="queryParams.childAgentNickName" placeholder="请输入所属二级代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd(true, false)"
          v-hasPermi="['ecs:payEase:add']">新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['ecs:payEase:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['ecs:payEase:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ecs:payEase:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payEaseList" @selection-change="handleSelectionChange">
      <el-table-column label="店铺名称" fixed="left" align="center" prop="signedShorthand" width="280">
        <template slot-scope="{ row }">
          <template v-if="row.signedShorthand">
            <span style="cursor: pointer; white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;">{{ row.signedShorthand }}</span>
            <br />
          </template>
          费率：{{ row.feeRate }}%
          <span v-if="user.userId === 1">- {{ row.rate }}%</span>
          <template v-if="row.subMerchantId">
            <br />
            商户号：{{ row.subMerchantId }}
          </template>
        </template>
      </el-table-column>
      <!--      <el-table-column label="商户号" align="center" prop="subMerchantId"  width="100"/>-->
      <el-table-column label="公司名称" align="center" prop="signedName" width="300" />
      <el-table-column label="所属平台" v-if="user.userId === 1" align="center" prop="platformNickName" width="210" />
      <el-table-column label="所属代理" v-if="user.deptId < 101" align="center" prop="agentNickName" width="210" />
      <el-table-column label="所属二级代理" v-if="user.deptId < 102" align="center" prop="childAgentNickName" width="210" />
      <el-table-column label="所属商户" align="center" prop="shopNickName" width="210" />
      <el-table-column label="商户类型" align="center" prop="registerRole" width="100">
        <template slot-scope="{ row }">
          {{ row.registerRole === "ENTERPRISE_LEGAL_PERSON" ? "企业" : "个体户" }}
        </template>
      </el-table-column>
      <el-table-column label="结算类型" align="center" prop="accountType" width="100">
        <template slot-scope="{ row }">
          {{ row.accountType === "PUBLIC" ? "对公" : "对私" }}
        </template>
      </el-table-column>
      <el-table-column label="法人姓名" align="center" prop="legalPersonName" width="110" />
      <el-table-column label="法人手机号" align="center" prop="legalPersonPhone" width="110" />
      <el-table-column label="联系人邮箱" align="center" prop="contactEmail" width="200" />
      <!--      <el-table-column label="费率" fixed="right" align="center" prop="feeRate" width="110">-->
      <!--        <template slot-scope="{ row }">-->
      <!--          {{ row.feeRate }}%-->
      <!--          <span v-if="user.userId === 1">- {{row.rate}}%</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="状态" fixed="right" align="center" prop="status" width="110">
        <template slot-scope="{ row }">
          {{ row.status === 1 ? "待提交" : row.status === 2 ? "审核中" : row.status === 3 ? "审核通过" : row.status === 4 ? "审核驳回"
            : row.status === 5 ? "入网成功" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="签约状态" fixed="right" align="center" prop="signingStatus" width="110">
        <template slot-scope="{ row }">
          {{ row.signingStatus === 0 ? "待签约" : row.signingStatus === 1 ? "签约中" : row.signingStatus === 2 ? "签约失败" :
            row.signingStatus === 3 ? "签约成功" : "" }}
        </template>
      </el-table-column>

      <el-table-column label="开户银行号" show-overflow-tooltip align="center" prop="accountNumber" width="230" />
      <el-table-column label="审核/签约失败原因" show-overflow-tooltip align="center" prop="reason" width="180" />
      <el-table-column label="提交时间" align="center" prop="incomingDate" width="180" />
      <el-table-column label="操作" fixed="right" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" :disabled="!(scope.row.status === 1 || scope.row.status === 4)"
            icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['ecs:payEase:edit']">修改</el-button>
          <el-button size="mini" type="text" v-if="scope.row.status === 5" icon="el-icon-edit"
            @click="handleUpdateFeeRate(scope.row)" v-hasPermi="['ecs:payEase:editFeeRate']">修改费率</el-button>
          <el-button size="mini" type="text" icon="el-icon-more-outline" @click="handDetail(scope.row)"
            v-hasPermi="['ecs:payEase:query']">详情</el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['ecs:payEase:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <PayEaseOperate v-if="open" @close="handleAdd" :formData="form" :opens="open"></PayEaseOperate>
    <PayEaseDetail v-if="openDetail" @close="handleDetailClose" :formData="form" :opens="openDetail"></PayEaseDetail>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="'修改 ' + title + ' 费率'"
      :visible.sync="openFeeRate" width="35%" append-to-body>
      <el-form v-loading="loading" ref="form" :model="form" label-width="110px" class="form">
        <el-form-item label="费率" prop="feeRate">
          <el-input-number :step="0.1" :min="0" :max="100" :precision="2" v-model.number="form.feeRate"
            placeholder="请输入费率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PayEaseOperate from './modules/operate.vue'
import PayEaseDetail from './modules/detail.vue'

import { listPayEase, getPayEase, delPayEase, editFeeRate } from "@/api/ecs/payEase";
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'
export default {
  name: "PayEase",
  components: {
    PayEaseOperate,
    PayEaseDetail
  },
  data() {
    return {
      statusOptions: [
        { value: 1, label: "待提交" },
        { value: 2, label: "审核中" },
        { value: 3, label: "审核通过" },
        { value: 4, label: "审核驳回" },
        { value: 5, label: "入网成功" },
      ],
      signingStatusOptions: [
        { value: 0, label: "待签约" },
        { value: 1, label: "签约中" },
        { value: 2, label: "签约失败" },
        { value: 3, label: "签约成功" },
      ],
      // 遮罩层
      loading: true,
      openDetail: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 首信易进件表格数据
      payEaseList: [],
      // 弹出层标题
      // 是否显示弹出层
      open: false,
      title: "",
      openFeeRate: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        registerRole: null,
        signedName: "",
        signedShorthand: null,
        subMerchantId: null,
        legalPersonName: null,
        legalPersonPhone: null,
        contactEmail: null,
        status: null,
        signingStatus: null,
        platformNickName: null,
        agentNickName: null,
        childAgentNickName: null,
        requestId: null,
      },
      // 表单参数
      form: {},
      user: {}
      // 表单校验
    };
  },
  created() {
    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101) {
        getAgentUserId(this.user.userId).then(response => {
          if (response.data.agentId !== 0) {
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  methods: {
    /** 查询首信易进件列表 */
    getList() {
      this.loading = true;
      listPayEase(this.queryParams).then(response => {
        this.payEaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openFeeRate = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(val1, val2) {
      this.open = val1;
      if (val2) {
        this.getList()
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      const id = row.id || this.ids
      getPayEase(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改首信易进件";
      });
    },
    handleUpdateFeeRate(row) {
      this.openFeeRate = true;
      this.title = row.signedShorthand
      this.form = {
        id: row.id,
        feeRate: row.feeRate
      }
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除首信易进件编号为"' + ids + '"的数据项？').then(function () {
        return delPayEase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/payEase/export', {
        ...this.queryParams
      }, `蓝海银行_${new Date().getTime()}.xlsx`)
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          editFeeRate(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openFeeRate = false;
            this.getList();
          });
        }
      });
    },
    handDetail(row) {
      this.loading = true
      const id = row.id || this.ids
      getPayEase(id).then(response => {
        this.loading = false
        this.form = response.data;
        this.openDetail = true;
        this.title = "首信易进件";
      }).catch(() => {
        this.loading = false
      })
    },
    handleDetailClose() {
      this.openDetail = false;
    }
  }
};
</script>
